﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AppConfiguration.DataAccess;
using AppConfigurationMVC.Models;

namespace AppConfigurationMVC.Controllers
{
    public class ConfigController : Controller
    {
        //
        // GET: /Config/

        public ActionResult Index()
        {
            DataHelper dh = new DataHelper();
            var model = dh.GetConfigs().OrderBy(c => c.ConfigKey);
            return View(model);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ConfigurationModels config)
        {
            DataHelper dh = new DataHelper();
            dh.CreateConfig(config);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            DataHelper dh = new DataHelper();
            var model = dh.GetConfigs().Single(c => c.ConfigID == id);

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(ConfigurationModels config)
        {
            DataHelper dh = new DataHelper();
            dh.UpdateConfig(config);

            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id)
        {
            DataHelper dh = new DataHelper();
            dh.DeleteConfig(id);

            return RedirectToAction("Index");
        }
    }
}
